package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 查询全部列表数据
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){

        try {
            List<Brand> brandList = brandService.findAll();
            return new Result(true, StatusCode.OK,"查询成功",brandList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败",null);
        }

    }

    /**
     * 根据id查询数据
     * @param id select * from 表名 where id ={id}
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
            Brand brand = brandService.findById(id);
            return new Result(true,StatusCode.OK,"查询成功",brand);

    }

    /**
     * 增加一行数据
     * @param brand  insert into 表名 (列名) values (列值)
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"新增成功");
    }

    /**
     *
     * @param brand update 表名 set (列名=更新值) where id = {id}
     * @return
     */
    @PutMapping(value = "/update")
    public Result updateBrand(@RequestBody Brand brand){

        try {
            brandService.updateBrand(brand);
            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }


    /**
     *
     * @param id update 表名 set (列名=更新值) where id = {id}
     * @param brand
     * @return
     */
    @PutMapping(value = "/update/{id}")
    public Result updateBrand1(@PathVariable Integer id,@RequestBody Brand brand){

        try {
            brand.setId(id);
            brandService.updateBrand(brand);
            return new Result(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }

    /**
     *
     * @param id 根据id删除 delete from 表名 where id= {id}
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result deleteBrand1(@PathVariable Integer id){

        try {
            brandService.deleteBrand(id);
            return new Result(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }

    /**
     * 条件查询 select * from 表名 where name={name} and letter={letter}
     * @param conditionMap 表示从请求中获取参数,并且赋值给map集合
     * @return
     */
    @GetMapping("/findByCondition")
    public Result findByCondition(@RequestParam Map conditionMap){
        List<Brand> brandList = brandService.findByCondition(conditionMap);
        if (brandList.size()<=0||brandList==null){
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }


    /**
     * 如果参数是实体的话,不需要加注解
     * @param brand
     * @return
     */
    @GetMapping("/findByCondition2")
    public Result findByCondition2(Brand brand){
        List<Brand> brandList = brandService.findByCondition2(brand);
        //int i=3/0;
        if (brandList.size()<=0||brandList==null){
            return new Result(false,StatusCode.ERROR,"查询失败");
        }
        return new Result(true,StatusCode.OK,"查询成功",brandList);
    }

    /**
     * 单独分页
     * @param page 第几条开始
     * @param size 每页显示几条
     * @return
     * warning Page<T> 是com.github.pagehelper.Page；
     */
    @GetMapping("/pageFindAll/{page}/{size}")
    public PageResult pageFindAll(@PathVariable Integer page, @PathVariable Integer size){
        try {
            Page<Brand> brandPage = brandService.findByPage(page, size);
            long total = brandPage.getTotal();
            List<Brand> brandList = brandPage.getResult();
            return new PageResult(total,brandList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(null,null);
        }
    }

    @GetMapping("/pageConditionFind/{page}/{size}")
    public PageResult pageConditionFind(@PathVariable Integer page,@PathVariable Integer size,Brand brand){
        try {
            Page<Brand> pageByCondition = brandService.findPageByCondition(page, size, brand);
            long total = pageByCondition.getTotal();
            List<Brand> brandList = pageByCondition.getResult();
            return new PageResult(total,brandList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(null,null);
        }
    }

}
